/***********************************************************
* 
*     File Name:  publish.cpp
* 
*        Author:  Algolc
* 
*          mail:  17360224@qq.com
* 
*  Created Time:  Mon 09 Jul 2018 06:48:37 PM CST
* 
***********************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include <string.h>
#include <zmq.h>
 
int main(int argc, char** argv) {
    if (argc < 2) {
        printf("Usage: %s [COUNT]\n", argv[0]);
        return -1;
    }
 
    int count = atoi(argv[1]);
    printf("start %d!\n", count);
 
    void* connect = zmq_ctx_new();                          //创建新的环境
    assert(NULL != connect);
 
    int ret = zmq_ctx_set(connect, ZMQ_MAX_SOCKETS, 1);     //在这个环境中只允许有一个socket的存在
    assert(!ret);
 
    void* publisher = zmq_socket(connect, ZMQ_PUB);         //创建发布者
    assert(NULL != publisher);
 
    ret = zmq_bind(publisher, "tcp://*:5556");              //bind
    assert(!ret);
 
    int send_count = 1;
 
    sleep(10);
    for (int i = 0; i < count; ++i) {
        char buf[16] = {0};
        sprintf(buf, "%d", send_count++);
 
        ret = zmq_send(publisher, buf, strlen(buf), 0);           
 
        //printf("ret = %d\n", ret);
        printf("%s\n", buf);
        usleep(1);
    }
 
    usleep(100);
    printf("success\n");
    return 0;
}
